package com.shanghaionstar.weight;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathEffect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.LinearInterpolator;

/**
 * Created by Yonglang Hong on 2016/10/18.
 */
public class DialPlateViewBlue extends View {

    private Paint mPointerPaint = new Paint();
    private Paint circlePaint = new Paint();
    private PathEffect effect;

    private static final float startAngel = 135;

    private static final float endAngel   = 405;

    private float betweenAngel = endAngel - startAngel;

    private float sweepAngle = 0;

    private float cx,cy;

    private static int L_BLUE = 0xFF83D0FC;
    private static int BLUE = 0xFF18ACFE;

    public DialPlateViewBlue(Context context) {
        super(context);
        init();
    }

    public DialPlateViewBlue(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        setFocusable(true);
        setFocusableInTouchMode(true);

        effect = new DashPathEffect(new float[]{2, 20}, 0);

        circlePaint.setStyle(Paint.Style.STROKE);
        circlePaint.setAntiAlias(true);

        mPointerPaint.setColor(BLUE);
        mPointerPaint.setStrokeWidth(3);
        mPointerPaint.setAntiAlias(true);
        mPointerPaint.setStyle(Paint.Style.STROKE);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        cx = getMeasuredWidth() / 2;
        cy = getMeasuredHeight() / 2;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawColor(Color.WHITE);
        drawPointer(canvas, cx,cy,startAngel);
        drawDegreeDial(canvas, cx, cy, cy, startAngel);
// for test

//        if (sweepAngle < 270) {
//            sweepAngle += 10;
//            invalidate();
//        }
    }

    private void drawDegreeDial(Canvas canvas, float cx, float cy, float r, float startAngel){
        circlePaint.setColor(L_BLUE);
        circlePaint.setPathEffect(null);
        circlePaint.setStrokeWidth(5);
        RectF rectF = new RectF(5, 5, cx + r-5 , cy + r-5);
        canvas.drawArc(rectF, startAngel, endAngel - startAngel, false, circlePaint);
        float lineDis = (float)(r * Math.sin(45 * Math.PI / 180));
        canvas.drawLine(r - lineDis - 30,cy + lineDis - 5,r - lineDis,cy + lineDis - 5,circlePaint);
        canvas.drawLine(r + lineDis ,cy + lineDis - 5,r + lineDis + 30,cy + lineDis - 5,circlePaint);
        circlePaint.setColor(BLUE);
        circlePaint.setPathEffect(null);
        circlePaint.setStrokeWidth(30);
        rectF = new RectF(35, 35, cx + r - 35, cy + r - 35);
        canvas.drawArc(rectF, startAngel, sweepAngle, false, circlePaint);
        circlePaint.setPathEffect(null);
        circlePaint.setStrokeWidth(5);
        rectF = new RectF(65, 65, cx + r - 65, cy + r - 65);
        canvas.drawArc(rectF, startAngel, endAngel - startAngel, false, circlePaint);
        circlePaint.setStrokeWidth(20);
        circlePaint.setPathEffect(effect);
        rectF = new RectF(75, 75, cx + r - 75, cy + r -75);
        canvas.drawArc(rectF, startAngel, endAngel - startAngel, false, circlePaint);
    }

    /**
     * 根据值显示
     */
    public void showByValue(float sweepValue) {
        if (sweepValue > 270 || sweepValue < 0)
            throw new IllegalArgumentException("输入的角度不在表盘区间内");
        ValueAnimator animator = ValueAnimator.ofFloat(0,270, sweepValue);
        animator.setInterpolator(new LinearInterpolator());
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                sweepAngle = (Float) animation.getAnimatedValue();
                invalidate();
            }
        });
        animator.setDuration(1000);
        animator.setStartDelay(100);
        animator.start();
    }

    /**
     * 根据百分比显示
     * @param sweepPercent
     */
    public void showByPercent(float sweepPercent) {
        if (sweepPercent > 1 || sweepPercent < 0)
            throw new IllegalArgumentException("输入的角度不在表盘区间内");
        ValueAnimator animator = ValueAnimator.ofFloat(0, sweepPercent);
        animator.setInterpolator(new LinearInterpolator());
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                sweepAngle = (Float) animation.getAnimatedValue() * betweenAngel;
                invalidate();
            }
        });
        animator.setDuration(800);
        animator.setStartDelay(100);
        animator.start();
    }

    /**
     * 绘制指针
     */
    private void drawPointer(Canvas canvas, float cx, float cy, float startAngel) {
        mPointerPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        canvas.drawCircle(cx, cy, 20, mPointerPaint);
        mPointerPaint.setStyle(Paint.Style.STROKE);
        canvas.drawCircle(cx, cy, 30, mPointerPaint);
        Path path = new Path();
        path.moveTo(cx, cy);
        float r = cx - 50 - 80;
        path.lineTo((float) (cx + r * Math.cos((startAngel+sweepAngle) * Math.PI / 180))
                , (float) (cy + r * Math.sin((startAngel+sweepAngle) * Math.PI / 180)));
        canvas.drawPath(path, mPointerPaint);
    }

}